<?php
require_once('./includes/headers.inc.php');

$error = FALSE;

$postLoginUrl = (isset($_SESSION['post_login']) ? $_SESSION['post_login'] : '/');

if($_POST) {
    if($userMgr->login($_POST['username'], $_POST['password'])) {
        header('Location: ' . $postLoginUrl);
        exit();
    } else {
        $error = $msgMgr->get('login-error');
    }
}

if(isset($_SESSION['user'])) {
    //already logged in
    header('Location: ' . $postLoginUrl);
    exit();
}

if(isset($_GET['closed'])) {
    //special case for post-account-delete action since the session is destroyed after that
    $error = $msgMgr->get('account-delete-success');
} elseif(isset($_SESSION['msg'])) {
    $error = $_SESSION['msg'];
    unset($_SESSION['msg']);
}

$replaces = array(
    'username'          => (isset($_POST['username']) ? $_POST['username'] : ''),
    'error'             => $msgMgr->formatMsg($error),
);
$lang = array(
    'text_email'            => $langMgr->get('text_email'),
    'text_password'         => $langMgr->get('text_password'),
    'btn_login'             => $langMgr->get('btn_login'),
    'link_register'         => $langMgr->get('link_register'),
    'link_forgotpassword'   => $langMgr->get('link_forgotpassword'),
);
$templateMgr->loadContent(basename(__FILE__, '.php'), array_merge($replaces, $lang));
?>
